home *** CD-ROM | disk | FTP | other *** search
/ Aminet 30 / Aminet 30 (1999)(Schatztruhe)[!][Apr 1999].iso / Aminet / gfx / misc / gnuplot-3.7src.lha / gnuplot-3.7src / gnuplot-3.7.lha / gnuplot-3.7 / win / winmain.c < prev    next >
C/C++ Source or Header  |  1998-12-04  |  11KB  |  487 lines

  1. #ifndef lint
  2. static char *RCSid = "$Id: winmain.c,v 1.15 1998/03/22 23:32:01 drd Exp $";
  3. #endif
  4.  
  5. /* GNUPLOT - win/winmain.c */
  6. /*[
  7.  * Copyright 1992, 1993, 1998   Maurice Castro, Russell Lang
  8.  *
  9.  * Permission to use, copy, and distribute this software and its
  10.  * documentation for any purpose with or without fee is hereby granted,
  11.  * provided that the above copyright notice appear in all copies and
  12.  * that both that copyright notice and this permission notice appear
  13.  * in supporting documentation.
  14.  *
  15.  * Permission to modify the software is granted, but not the right to
  16.  * distribute the complete modified source code.  Modifications are to
  17.  * be distributed as patches to the released version.  Permission to
  18.  * distribute binaries produced by compiling modified sources is granted,
  19.  * provided you
  20.  *   1. distribute the corresponding source modifications from the
  21.  *    released version in the form of a patch file along with the binaries,
  22.  *   2. add special version identification to distinguish your version
  23.  *    in addition to the base release version number,
  24.  *   3. provide your name and address as the primary contact for the
  25.  *    support of your modified version, and
  26.  *   4. retain our contact information in regard to use of the base
  27.  *    software.
  28.  * Permission to distribute the released version of the source code along
  29.  * with corresponding source modifications in the form of a patch file is
  30.  * granted with same provisions 2 through 4 for binary distributions.
  31.  *
  32.  * This software is provided "as is" without express or implied warranty
  33.  * to the extent permitted by applicable law.
  34. ]*/
  35.  
  36. /*
  37.  * AUTHORS
  38.  * 
  39.  *   Maurice Castro
  40.  *   Russell Lang
  41.  * 
  42.  * Send your comments or suggestions to 
  43.  *  info-gnuplot@dartmouth.edu.
  44.  * This is a mailing list; to join it send a note to 
  45.  *  majordomo@dartmouth.edu.  
  46.  * Send bug reports to
  47.  *  bug-gnuplot@dartmouth.edu.
  48.  */
  49.  
  50. /* This file implements the initialization code for running gnuplot   */
  51. /* under Microsoft Windows. The code currently compiles only with the */
  52. /* Borland C++ 3.1 compiler. 
  53. /*                                                                    */
  54. /* The modifications to allow Gnuplot to run under Windows were made  */
  55. /* by Maurice Castro. (maurice@bruce.cs.monash.edu.au)  3 Jul 1992    */
  56. /* and Russell Lang (rjl@monu1.cc.monash.edu.au) 30 Nov 1992          */
  57. /*                                                                    */
  58.  
  59. #define STRICT
  60. #include <windows.h>
  61. #include <windowsx.h>
  62. #include <dos.h>
  63. #include <stdio.h>
  64. #include <stdlib.h>
  65. #include <string.h>
  66. #include <stdarg.h>
  67. #include <ctype.h>
  68. #ifdef __MSC__
  69. #include <malloc.h>
  70. #else
  71. # ifdef __TURBOC__ /* HBB 981201: MinGW32 doesn't have this */
  72. #include <alloc.h>
  73. #endif
  74. #endif
  75. #include <io.h>
  76. #include "plot.h"
  77. #include "setshow.h"
  78. #include "wgnuplib.h"
  79. #include "wtext.h"
  80.  
  81. /* limits */
  82. #define MAXSTR 255
  83. #define MAXPRINTF 1024
  84.  
  85. /* globals */
  86. TW textwin;
  87. GW graphwin;
  88. PW pausewin;
  89. MW menuwin;
  90. LPSTR szModuleName;
  91. LPSTR winhelpname;
  92. LPSTR szMenuName;
  93. #define MENUNAME "wgnuplot.mnu"
  94. #ifndef HELPFILE /* HBB 981203: makefile.win predefines this... */
  95. #define HELPFILE "wgnuplot.hlp"
  96. #endif
  97.  
  98. extern char version[];
  99. extern char patchlevel[];
  100. extern char date[];
  101. /*extern char *authors[];*/
  102. char *authors[]={
  103.                  "Colin Kelly",
  104.                  "Thomas Williams"
  105.                 };
  106.  
  107. extern char gnuplot_copyright[];
  108. void WinExit(void);
  109. int gnu_main(int argc, char *argv[], char *env[]);
  110.  
  111. void
  112. CheckMemory(LPSTR str)
  113. {
  114.     if (str == (LPSTR)NULL) {
  115.         MessageBox(NULL, "out of memory", "gnuplot", MB_ICONSTOP | MB_OK);
  116.         exit(1);
  117.     }
  118. }
  119.  
  120. int
  121. Pause(LPSTR str)
  122. {
  123.     pausewin.Message = str;
  124.     return (PauseBox(&pausewin) == IDOK);
  125. }
  126.  
  127. /* atexit procedure */
  128. void
  129. WinExit(void)
  130. {
  131.     term_reset();
  132.  
  133. #ifndef __MINGW32__ /* HBB 980809: FIXME: doesn't exist for MinGW32. So...? */
  134.     fcloseall();
  135. #endif
  136.     if (graphwin.hWndGraph && IsWindow(graphwin.hWndGraph))
  137.         GraphClose(&graphwin);
  138.     TextMessage();    /* process messages */
  139.      WinHelp(textwin.hWndText,(LPSTR)winhelpname,HELP_QUIT,(DWORD)NULL);
  140.     TextClose(&textwin);
  141.     TextMessage();    /* process messages */
  142.     return;
  143. }
  144.  
  145. /* call back function from Text Window WM_CLOSE */
  146. int CALLBACK WINEXPORT
  147. ShutDown(void)
  148. {
  149.     WinExit();
  150.     exit(0);
  151.     return 0;
  152. }
  153.  
  154. int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  155.         LPSTR lpszCmdLine, int nCmdShow)
  156. {
  157.     /*WNDCLASS wndclass;*/
  158.     LPSTR tail;
  159.     
  160. #ifdef __MSC__  /* MSC doesn't give us _argc and _argv[] so ...   */
  161. #define MAXCMDTOKENS 128
  162.     int     _argc=0;
  163.     LPSTR   _argv[MAXCMDTOKENS];
  164.     _argv[_argc] = "wgnuplot.exe";
  165.     _argv[++_argc] = _fstrtok( lpszCmdLine, " ");
  166.     while (_argv[_argc] != NULL)
  167.         _argv[++_argc] = _fstrtok( NULL, " ");
  168. #endif /* __MSC__ */
  169.  
  170.     szModuleName = (LPSTR)farmalloc(MAXSTR+1);
  171.     CheckMemory(szModuleName);
  172.  
  173.     /* get path to EXE */
  174.     GetModuleFileName(hInstance, (LPSTR) szModuleName, MAXSTR);
  175. #ifndef WIN32
  176.     if (CheckWGNUPLOTVersion(WGNUPLOTVERSION)) {
  177.         MessageBox(NULL, "Wrong version of WGNUPLOT.DLL", szModuleName, MB_ICONSTOP | MB_OK);
  178.         exit(1);
  179.     }
  180. #endif
  181.     if ((tail = (LPSTR)_fstrrchr(szModuleName,'\\')) != (LPSTR)NULL)
  182.     {
  183.         tail++;
  184.         *tail = 0; 
  185.     }
  186.     szModuleName = (LPSTR)farrealloc(szModuleName, _fstrlen(szModuleName)+1);
  187.     CheckMemory(szModuleName);
  188.  
  189.     winhelpname = (LPSTR)farmalloc(_fstrlen(szModuleName)+_fstrlen(HELPFILE)+1);
  190.     CheckMemory(winhelpname);
  191.     _fstrcpy(winhelpname,szModuleName);
  192.     _fstrcat(winhelpname,HELPFILE);
  193.  
  194.     szMenuName = (LPSTR)farmalloc(_fstrlen(szModuleName)+_fstrlen(MENUNAME)+1);
  195.     CheckMemory(szMenuName);
  196.     _fstrcpy(szMenuName,szModuleName);
  197.     _fstrcat(szMenuName,MENUNAME);
  198.  
  199.     textwin.hInstance = hInstance;
  200.     textwin.hPrevInstance = hPrevInstance;
  201.     textwin.nCmdShow = nCmdShow;
  202.     textwin.Title = "gnuplot";
  203.     textwin.IniFile = "wgnuplot.ini";
  204.     textwin.IniSection = "WGNUPLOT";
  205.     textwin.DragPre = "load '";
  206.     textwin.DragPost = "'\n";
  207.     textwin.lpmw = &menuwin;
  208.     textwin.ScreenSize.x = 80;
  209.     textwin.ScreenSize.y = 80;
  210.     textwin.KeyBufSize = 2048;
  211.     textwin.CursorFlag = 1;    /* scroll to cursor after \n & \r */
  212.     textwin.shutdown = MakeProcInstance((FARPROC)ShutDown, hInstance);
  213.     textwin.AboutText = (LPSTR)farmalloc(1024);
  214.     CheckMemory(textwin.AboutText);
  215.     sprintf(textwin.AboutText,"Version %s\nPatchlevel %s\nLast Modified %s\n%s\n%s, %s and many others",
  216.         version, patchlevel, date, gnuplot_copyright, authors[1], authors[0]);
  217.     textwin.AboutText = (LPSTR)farrealloc(textwin.AboutText, _fstrlen(textwin.AboutText)+1);
  218.     CheckMemory(textwin.AboutText);
  219.  
  220.     menuwin.szMenuName = szMenuName;
  221.  
  222.     pausewin.hInstance = hInstance;
  223.     pausewin.hPrevInstance = hPrevInstance;
  224.     pausewin.Title = "gnuplot pause";
  225.  
  226.     graphwin.hInstance = hInstance;
  227.     graphwin.hPrevInstance = hPrevInstance;
  228.     graphwin.Title = "gnuplot graph";
  229.     graphwin.lptw = &textwin;
  230.     graphwin.IniFile = textwin.IniFile;
  231.     graphwin.IniSection = textwin.IniSection;
  232.     graphwin.color=TRUE;
  233.     graphwin.fontsize = WINFONTSIZE;
  234.  
  235.     if (TextInit(&textwin))
  236.         exit(1);
  237.     textwin.hIcon = LoadIcon(hInstance, "TEXTICON");
  238. #ifdef WIN32
  239.     SetClassLong(textwin.hWndParent, GCL_HICON, (DWORD)textwin.hIcon);
  240. #else
  241.     SetClassWord(textwin.hWndParent, GCW_HICON, (WORD)textwin.hIcon);
  242. #endif
  243.     if (_argc>1) {
  244.         int i,noend=FALSE;
  245.         for (i=0; i<_argc; ++i)
  246.             if (!stricmp(_argv[i],"-noend") || !stricmp(_argv[i],"/noend"))
  247.                 noend = TRUE;
  248.         if (noend)
  249.             ShowWindow(textwin.hWndParent, textwin.nCmdShow);
  250.     }
  251.     else
  252.         ShowWindow(textwin.hWndParent, textwin.nCmdShow);
  253.     if (IsIconic(textwin.hWndParent)) { /* update icon */
  254.         RECT rect;
  255.         GetClientRect(textwin.hWndParent, (LPRECT) &rect);
  256.         InvalidateRect(textwin.hWndParent, (LPRECT) &rect, 1);
  257.         UpdateWindow(textwin.hWndParent);
  258.     }
  259.  
  260.  
  261.     atexit(WinExit);
  262.  
  263.     gnu_main(_argc, _argv, environ);
  264.  
  265.     return 0;
  266. }
  267.  
  268.  
  269. /* replacement stdio routines that use Text Window for stdin/stdout */
  270. /* WARNING: Do not write to stdout/stderr with functions not listed 
  271.    in win/wtext.h */
  272.  
  273. #undef kbhit
  274. #undef getche
  275. #undef getch
  276. #undef putch
  277.  
  278. #undef fgetc
  279. #undef getchar
  280. #undef getc
  281. #undef fgets
  282. #undef gets
  283.  
  284. #undef fputc
  285. #undef putchar
  286. #undef putc
  287. #undef fputs
  288. #undef puts
  289.  
  290. #undef fprintf
  291. #undef printf
  292. #undef vprintf
  293. #undef vfprintf
  294.  
  295. #undef fwrite
  296. #undef fread
  297.  
  298. #if defined(__MSC__)|| defined(WIN32)
  299. #define isterm(f) (f==stdin || f==stdout || f==stderr)
  300. #else
  301. #define isterm(f) isatty(fileno(f))
  302. #endif
  303.  
  304. int
  305. MyPutCh(int ch)
  306. {
  307.     return TextPutCh(&textwin, (BYTE)ch);
  308. }
  309.  
  310. int
  311. MyKBHit(void)
  312. {
  313.     return TextKBHit(&textwin);
  314. }
  315.  
  316. int
  317. MyGetCh(void)
  318. {
  319.     return TextGetCh(&textwin);
  320. }
  321.  
  322. int
  323. MyGetChE(void)
  324. {
  325.     return TextGetChE(&textwin);
  326. }
  327.  
  328. int
  329. MyFGetC(FILE *file)
  330. {
  331.     if (isterm(file)) {
  332.         return MyGetChE();
  333.     }
  334.     return fgetc(file);
  335. }
  336.  
  337. char *
  338. MyGetS(char *str)
  339. {
  340.     TextPutS(&textwin,"\nDANGER: gets() used\n");
  341.     MyFGetS(str,80,stdin);
  342.     if (strlen(str) > 0 
  343.      && str[strlen(str)-1]=='\n')
  344.         str[strlen(str)-1] = '\0';
  345.     return str;
  346. }
  347.  
  348. char *
  349. MyFGetS(char *str, unsigned int size, FILE *file)
  350. {
  351. char FAR *p;
  352.     if (isterm(file)) {
  353.         p = TextGetS(&textwin, str, size);
  354.         if (p != (char FAR *)NULL)
  355.             return str;
  356.         return (char *)NULL;
  357.     }    
  358.     return fgets(str,size,file);
  359. }
  360.  
  361. int
  362. MyFPutC(int ch, FILE *file)
  363. {
  364.     if (isterm(file)) {
  365.         MyPutCh((BYTE)ch);
  366.         TextMessage();
  367.         return ch;
  368.     }
  369.     return fputc(ch,file);
  370. }
  371.  
  372. int
  373. MyFPutS(char *str, FILE *file)
  374. {
  375.     if (isterm(file)) {
  376.         TextPutS(&textwin, str);
  377.         TextMessage();
  378.         return (*str);    /* different from Borland library */
  379.     }
  380.     return fputs(str,file);
  381. }
  382.  
  383. int
  384. MyPutS(char *str)
  385. {
  386.     TextPutS(&textwin, str);
  387.     MyPutCh('\n');
  388.     TextMessage();
  389.     return 0;    /* different from Borland library */
  390. }
  391.  
  392. int MyFPrintF(FILE *file, char *fmt, ...)
  393. {
  394. int count;
  395. va_list args;
  396.     va_start(args,fmt);
  397.     if (isterm(file)) {
  398.         char buf[MAXPRINTF];
  399.         count = vsprintf(buf,fmt,args);
  400.         TextPutS(&textwin,buf);
  401.     }
  402.     else
  403.         count = vfprintf(file, fmt, args);
  404.     va_end(args);
  405.     return count;
  406. }
  407.  
  408. int MyPrintF(char *fmt, ...)
  409. {
  410. int count;
  411. char buf[MAXPRINTF];
  412. va_list args;
  413.     va_start(args,fmt);
  414.     count = vsprintf(buf,fmt,args);
  415.     TextPutS(&textwin,buf);
  416.     va_end(args);
  417.     return count;
  418. }
  419.  
  420. size_t MyFWrite(const void *ptr, size_t size, size_t n, FILE *file)
  421. {
  422.     if (isterm(file)) {
  423.         size_t i;
  424.         for (i=0; i<n; i++)
  425.             TextPutCh(&textwin, ((BYTE *)ptr)[i]);
  426.         TextMessage();
  427.         return n;
  428.     }
  429.     return fwrite(ptr, size, n, file);
  430. }
  431.  
  432. size_t MyFRead(void *ptr, size_t size, size_t n, FILE *file)
  433. {
  434.     if (isterm(file)) {
  435.         size_t i;
  436.         for (i=0; i<n; i++)
  437.             ((BYTE *)ptr)[i] = TextGetChE(&textwin);
  438.         TextMessage();
  439.         return n;
  440.     }
  441.     return fread(ptr, size, n, file);
  442. }
  443.  
  444. /* public interface to printer routines : Windows PRN emulation
  445.  * (formerly in win.trm)
  446.  */
  447.  
  448. #define MAX_PRT_LEN 256
  449. static char win_prntmp[MAX_PRT_LEN+1];
  450.  
  451. extern GW graphwin;
  452.  
  453. FILE *
  454. open_printer()
  455. {
  456. char *temp;
  457.     if ((temp = getenv("TEMP")) == (char *)NULL)
  458.         *win_prntmp='\0';
  459.     else  {
  460.         strncpy(win_prntmp,temp,MAX_PRT_LEN);
  461.         /* stop X's in path being converted by mktemp */
  462.         for (temp=win_prntmp; *temp; temp++)
  463.             *temp = tolower(*temp);
  464.         if ( strlen(win_prntmp) && (win_prntmp[strlen(win_prntmp)-1]!='\\') )
  465.             strcat(win_prntmp,"\\");
  466.     }
  467.     strncat(win_prntmp, "_gptmp",MAX_PRT_LEN-strlen(win_prntmp));
  468.     strncat(win_prntmp, "XXXXXX",MAX_PRT_LEN-strlen(win_prntmp));
  469.     mktemp(win_prntmp);
  470.     return fopen(win_prntmp, "w");
  471. }
  472.  
  473. void
  474. close_printer(FILE *outfile)
  475. {
  476.     fclose(outfile);
  477.     DumpPrinter(graphwin.hWndGraph, graphwin.Title, win_prntmp);
  478. }
  479.  
  480. void
  481. screen_dump(void)
  482. {
  483.     GraphPrint(&graphwin);
  484. }
  485.  
  486.  
  487.